package com.aurora.source.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.util.ErrorImgUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.photoview.PhotoView;

import java.util.ArrayList;
import java.util.Locale;

import butterknife.BindView;

/**
 * @author: ww
 * @description:
 * @date: 2018/10/15
 */
public class PhotoViewActivity extends BaseWhiteToolbarActivity {
    @BindView(R.id.vp_show)
    ViewPager vpShow;
    String url;
    String title;
    int position;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    private ArrayList<String> mList;//图片list

    @Override
    public BasePresenter attach() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_photo_view;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        mList = getIntent().getStringArrayListExtra("images");
        title = getIntent().getStringExtra("title");
        position = getIntent().getIntExtra("position", 0);
        tvNumber.setText(String.format(Locale.getDefault(),"%d/%d", position + 1, mList.size()));
        simpleToolbar.setMainTitle(title);
        simpleToolbar.setLeftVisibility(View.VISIBLE);
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        vpShow.setAdapter(new SeePagerAdapter());
        vpShow.setCurrentItem(position);
        vpShow.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvNumber.setText(String.format(Locale.getDefault(),"%d/%d", position + 1, mList.size()));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void doBusiness() {

    }



    //ViewPager的主体部分
    class SeePagerAdapter extends PagerAdapter {

        /**
         * 返回多少page
         */
        public int getCount() {
            return mList.size();
        }

        /**
         * view滑动到一半时是否创建新的view
         * true:表示不去创建，使用缓存；false:去重新创建
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        /**
         * 类似于BaseAdapter的getView方法
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = null;
            try {
                view = View.inflate(activity, R.layout.item_see_image, null);
                PhotoView imageView = view.findViewById(R.id.iv_see_image);
                String url = mList.get(position);
                Glide.with(activity)
                        .load(url)
                        .apply(new RequestOptions().placeholder(R.drawable.ic_default).error(ErrorImgUtil.getErrorImgRandom()))
                        .into(imageView);
                container.addView(view);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return view;
        }

        /**
         * @param position:当前需要销毁第几个page
         * @param object:当前需要销毁的page
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }

}
